<template>
    <el-container>
        <el-main class="wrapper">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{'min-height': mainHeight+'px'}">
                <div class="panel-body">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="create" type="primary" icon="fa fa-plus"
                                   size="small"> 新增
                        </el-button>
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                    </div>
                    <el-table  v-loading="loading"
                               element-loading-text="努力加载中..."
                               element-loading-spinner="el-icon-loading"
                               :data="tableData" :border="true" style="width: 100%">
                        <el-table-column prop="id" label="编号" width="80px"></el-table-column>
                        <el-table-column prop="name" label="角色" width="200px"></el-table-column>
                        <el-table-column prop="note" label="描述" width="200px"></el-table-column>
                        <el-table-column prop="created_at" label="创建时间" width="150px">
                            <template slot-scope="scope">{{ scope.row.created_at ? moment(scope.row.created_at).format('YYYY-MM-DD HH:mm') : ''}}</template>
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="edit(scope.row)" icon="fa fa-edit" plain>
                                    编辑</el-button>
                                <el-button type="primary" size="mini" @click="access(scope.row.id)" icon="fa fa-edit" plain>
                                    授权</el-button>
                                <el-button icon="fa fa-trash" v-if="scope.row.id > 1"
                                           @click="destory(scope.row.id)" type="danger" size="mini" plain> 删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination">
                        <div class="pagination-total">共 <em>{{pageData.total}}</em> 个角色</div>
                        <el-pagination  v-if="!loading && pageData && pageData.last_page > 1" background layout="prev, pager, next, jumper"
                                        :page-size="pageData.per_page"
                                        :total="pageData.total" :current-page="pageData.current_page"
                                        @current-change="handleCurrentChange">
                        </el-pagination>
                    </div>
                </div>
            </section>
        </el-main>
        <el-dialog
                :title="dialogTile"
                custom-class="bh-dialog"
                top="23vh"
                @open="resetForm('formData')"
                :visible.sync="updateDialog"
                width="500px">
            <el-form :model="formData" :rules="rules" ref="formData" label-width="100px" class="demo-ruleForm">
                <el-form-item required label="角色名称：" prop="name">
                    <el-input  v-model="formData.name" placeholder="请填写角色名称"></el-input>
                </el-form-item>
                <el-form-item label="角色描述：" prop="note">
                    <el-input v-model="formData.note" placeholder="请填写角色描述"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="updateDialog = false">取 消</el-button>
                <el-button type="primary" @click="update('formData')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import vBreadcrumb from '../layout/breadcrumb.vue';
    import {RoleList,RoleDestory,RoleUpdate} from '../../utils/axios';
    export default {
        components:{vBreadcrumb},
        data(){
            return {
                breadcumb:[{name: "角色管理"},],
                tableData:[],//列表数据
                pageData:[],//分页数据
                updateDialog:false,//表单弹窗
                dialogTile:'',//表单标题
                formData:{
                    id:0,
                    name:'',
                    note:'',
                },
                rules: {
                    name: [{ required: true, message: '请填写角色名称', trigger: 'blur' }],
                },
            }
        },
        activated(){
            this.$store.dispatch('setActive', '/role');
            this.list();
        },
        computed: {
            ...mapGetters(['active','mainHeight','loading'])
        },
        methods:{
            //分页
            handleCurrentChange(val) {
                this.search.page = val;
                this.list();
            },
            //获取列表
            list(){
                this.$store.dispatch('setLoading', true);
                RoleList().then((response)=>{
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.tableData = response.data.data;
                        this.pageData = response.data;
                    }
                })
            },
            //授权
            access(rid){
                this.$router.push({name:'Access',params:{id:rid}})
            },
            //新增
            create(){
                this.formData = {
                    id:0,
                    name:'',
                    note:'',
                };
                this.updateDialog = true;
                this.dialogTile = '新增角色';
            },
            //编辑
            edit(info){
                this.formData = {
                    id:info.id,
                    name: info.name,
                    note:info.note,
                };
                this.dialogTile = '编辑角色';
                this.updateDialog = true;
            },
            //更新
            update(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        RoleUpdate(this.formData).then((response)=>{
                            if(response.code){
                                this.$message.error(response.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: response.msg,
                                    onClose:() =>{
                                        this.updateDialog = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //删除
            destory(rid){
                this.$confirm('确定要删除该角色信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    RoleDestory({params:{id: rid}}).then((response) => {
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
            //重置表单
            resetForm(formName) {
                if (this.$refs[formName]!==undefined) {
                    this.$refs[formName].resetFields();
                }
            },
        },
    }
</script>
